package com.binhanh.controller.uploads;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.binhanh.common.Utils;
import com.binhanh.model.YoutubeVideo;
import com.binhanh.sql.bo.Video;

public class ExcelHandler {
	
	public static ArrayList<Video> readVideoExcel(String path){
		
		ArrayList<Video> lst = null;
		try
		{
			
			FileInputStream file = new FileInputStream(new File(path));
			
			

			//Create Workbook instance holding reference to .xlsx file
			XSSFWorkbook workbook = new XSSFWorkbook(file);

			//Get first/desired sheet from the workbook
			XSSFSheet sheet = workbook.getSheetAt(0);

			//Iterate through each rows one by one
			Iterator<Row> rowIterator = sheet.iterator();
			
			//Tạo đối tượng lưu trữ danh sách video
			lst = new ArrayList<Video>();
			Video video;
			
			//bỏ dòng tiêu đề đầu tiên
			Row row = rowIterator.next();
			
			while (rowIterator.hasNext()) 
			{
				row = rowIterator.next();
				video = new Video();
				//lấy tên
				video.setTitle(row.getCell(1).getStringCellValue());
				video.setUrl(row.getCell(2).getStringCellValue());
				video.setCategoryType((int)row.getCell(3).getNumericCellValue());
				video.youtubeId = Utils.extractYoutubeId(video.getUrl());
				lst.add(video);
				
			}
			file.close();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		return lst;
	}

}
